const Service = require('egg').Service;
const md5 = require("utility")
const jwt = require("jsonwebtoken")
class UserService extends Service {
    //登录
    async login() {
        const {ctx,app}=this;
        const {username,password}=ctx.request.body;
        const isHave = await app.mysql.get("user",{username,password:md5(String(password))})
        return {
            data:isHave,
            token:jwt.sign({isHave},app.config.keys)
        } 
    }
    //注册'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      ,
    async regisert(){
        const {ctx,app}=this;
        const {username,password,nickanme}=ctx.request.body;
        const isHave = await app.mysql.get("user",{username})
        if(isHave){
            return "注册成功"
        }else{
            const data = await app.mysql.insert("user",{username,nickanme,password:md5(String(password))})
            return data
        }
        
    }
}

module.exports = UserService;